from itsdangerous import TimedJSONWebSignatureSerializer as TJWSSerializer,BadData
from django.conf import settings

def generate_save_user_token(openid):
    """對openid進行加密"""
    #1. 創建加密的序列化對象
    serializer = TJWSSerializer(settings.SECRET_KEY,600)

    #2. 調用 dumps(JSON字典)方法進行加密
    data = {'openid':openid}
    token = serializer.dumps(data)

    #3. 把加載後的openid返回
    return token.decode()

def check_save_user_token(access_token):
    """傳入加密的openid進行解密並返回"""
    # 1.創建加密的序列化器對象secret_key
    serializers = TJWSSerializer(settings.SECERT_KET,600)

    # 2.調用 loads方法進行解密
    try:
        data = serializers.loads(access_token)
    except BadData:
        return None
    else:
        return data.get('openid')







